package com.jayqqaa12.system.controller;

import java.util.UUID;

import org.apache.shiro.SecurityUtils;

import com.jayqqaa12.Code;
import com.jayqqaa12.common.Consts;
import com.jayqqaa12.jbase.jfinal.ext.ShiroExt;
import com.jayqqaa12.jbase.jfinal.ext.ctrl.JsonController;
import com.jayqqaa12.jbase.util.Validate;
import com.jayqqaa12.service.EmailService;
import com.jayqqaa12.shiro.ShiroCache;
import com.jayqqaa12.system.model.User;
import com.jayqqaa12.system.validator.UserValidator;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.spring.Inject;

@ControllerBind(controllerKey = "/system/user")
public class UserCtrl extends JsonController<User> {

	@Inject.BY_TYPE
	User userDao;

	public void list() {
		sendJson("list", userDao.list(getParaToInt("p"), getParaToInt("c")));
	}

	@Override
	public void delete() {
		renderJsonResult(userDao.deleteById(getPara("id")));
	}

	public void freeze() {
		renderJsonResult(userDao.changeStaus(getParaToInt("id"), getParaToInt("status")));
	}

	public void batchDelete() {
		renderJsonResult(userDao.batchDelete(getPara("ids")));
	}

	public void batchGrant() {

		Integer[] role_ids = getParaValuesToInt("role_ids");
		String ids = getPara("ids");

		renderJsonResult(userDao.batchGrant(role_ids, ids));
		ShiroCache.clearAuthorizationInfoAll();

	}

	@Before(value = { UserValidator.class })
	public void save() {
		User u = getModel();

		if (u.getId() != null) u.encrypt().update();
		else SecurityUtils.getSubject().getSession().setAttribute(Consts.SESSION_EAMIL_USER, u);

		renderJson200();
	}


	public void getUser() {

		sendJson(userDao.getUser(getPara("id")));

	}
 

	public void grant() {
		Integer[] role_ids = getParaValuesToInt("role_ids");
		renderJsonResult(userDao.grant(role_ids, getModel().getId()));
		ShiroCache.clearAuthorizationInfoAll();

	}

}
